<template>
    <div class="layout-padding">
        <splitpanes horizontal>
            <pane size="50">
                <div class="layout-padding-auto layout-padding-view">
                    <!--查询表单-->
                    <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
                        <el-form-item :label="$t('poheader.status')" prop="status">
                            <el-select v-model="state.queryForm.status" style="width: 160px">
                                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('poheader.poCode')" prop="poCode">
                            <el-input v-model="state.queryForm.poCode" clearable/>
                        </el-form-item>
                        <el-form-item :label="$t('poheader.purDate')" prop="purDate">
                            <div class="block">
                                <el-date-picker
                                    clearable
                                    v-model="purDate"
                                    type="daterange"
                                    unlink-panels
                                    range-separator="To"
                                    start-placeholder="Start date"
                                    end-placeholder="End date"
                                    :shortcuts="shortcuts"
                                />
                            </div>
                        </el-form-item>
                        <el-form-item>
                            <el-button icon="el-icon-search" type="primary" @click="query">{{ $t('common.queryBtn') }}</el-button>
                            <el-button icon="refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
                        </el-form-item>
                    </el-form>
                    <el-table :data="state.dataList" v-loading="state.loading" highlight-current-row border
                              :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                              @row-click="clickRow" style="width: 100%;height: 100%;"
                              @selection-change="selectionChangeHandle">
                        <el-table-column type="index" label="#" width="60"/>
                        <el-table-column prop="status" :label="$t('poheader.status')" show-overflow-tooltip>
                            <template #default="scope">
                                <el-tag :type="scope.row.status === 'po_open'?'primary':'info' ">
                                    {{ scope.row.status === 'po_open' ? '订单开启' : '订单关闭' }}
                                </el-tag>
                            </template>
                        </el-table-column>s
                        <el-table-column prop="poCode" :label="$t('poheader.poCode')" show-overflow-tooltip/>
                        <el-table-column prop="purDate" :label="$t('poheader.purDate')" show-overflow-tooltip/>
                        <el-table-column prop="supplierCode" :label="$t('poheader.supplierCode')" show-overflow-tooltip/>
                        <el-table-column prop="supplierName" :label="$t('poheader.supplierName')" show-overflow-tooltip/>
                        <el-table-column prop="tenantId" :label="$t('poheader.tenantId')" show-overflow-tooltip/>
                        <el-table-column prop="creationTime" :label="$t('poheader.creationTime')" show-overflow-tooltip/>
                    </el-table>
                    <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
                                v-bind="state.pagination"/>
                </div>
            </pane>
            <pane size="50">
                <el-tabs model-value="first" class=" card-tabs" type="border-card">
                    <el-tab-pane name="first" class="">
                        <template #label>
                            <el-icon><calendar/></el-icon>
                            <span class="ml-0.5"> {{ $t('poheader.orderDetail') }} </span>
                        </template>
											<srmPodetail ref="locationRef"/>
                    </el-tab-pane>
                </el-tabs>
            </pane>
        </splitpanes>
    </div>
</template>

<script setup lang="ts" name="systemSrmPoheader">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {fetchList} from "/@/api/platform/srm/srmPoheader";
import srmPodetail from "/src/views/srm/srmPoheader/srmPodetail/index.vue";
import {ref,reactive } from 'vue';
import {ElMessage} from "element-plus";


const getYMD = (date:any) => {
    // 获取年份
    const year = date.getFullYear();

    // 获取月份，注意JavaScript中的月份是从0开始的，因此需要+1
    const month = String(date.getMonth() + 1).padStart(2, '0');

    // 获取日期
    const day = String(date.getDate()).padStart(2, '0');

    // 拼接成 yyyy-MM-dd 格式的字符串
    const formattedDate = `${year}-${month}-${day}`;

    return formattedDate;
};

const today = new Date();
const endDate = new Date(today);
endDate.setDate(today.getDate() - 6); // 今天之后的6天，总共一周
const purDate = ref([endDate, today]);

const shortcuts = [
    {
        text: 'Last week',
        value: () => {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            return [start, end];
        },
    },
    {
        text: 'Last month',
        value: () => {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            return [start, end];
        },
    },
    {
        text: 'Last 3 months',
        value: () => {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            return [start, end];
        },
    },
];

const options = ref([
    { label: '全部', value: '' },
    { label: '订单开启', value: 'po_open' },
    { label: '订单关闭', value: 'po_close' },
]);

// 搜索变量
const queryRef = ref()
const locationRef = ref()


const state: BasicTableProps = reactive<BasicTableProps>({
    queryForm: {
        startDate : purDate.value ? getYMD(purDate.value[0]) : '',
        endDate : purDate.value ? getYMD(purDate.value[1]) : '',
    },
    pageList: fetchList
})



//  table hook
const {
    getDataList,
    currentChangeHandle,
    sizeChangeHandle,
    tableStyle
} = useTable(state)

// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

// 多选事件
const selectionChangeHandle = (objs: { id: string }[]) => {
    selectObjs.value = objs.map(({id}) => id);
    multiple.value = !objs.length;
};

//查询
const query = () => {
    if (!purDate.value || !purDate.value[0]) {
        ElMessage.error('采购日期必填'); // 使用Element Plus的message组件提示用户
        return; // 如果开始时间未选，则不继续执行后续逻辑
    }
    state.queryForm.startDate = purDate.value ? getYMD(purDate.value[0]) : '';
    state.queryForm.endDate = purDate.value ? getYMD(purDate.value[1]) : '';
    getDataList();
};

// 清空搜索条件
const resetQuery = () => {
    state.queryForm.status = "";
    state.queryForm.poCode = null;
		state.queryForm.startDate=''
		state.queryForm.endDate=''
		purDate.value=[]
    // 清空日期选择器的值
    // state.queryForm.purDate.value = null;

		getDataList(false)
    // 清空搜索条件
    queryRef.value?.resetFields()
}
// 点击行的时候触发
const clickRow = (row: any) => {
    locationRef.value.query(row)
}
</script>
